'''
字典推导式

'''
#1 2 3 4 5 。。。 -> 平方
dict01 = {}
for item in range(1,11):
    dict01[item] = item**2

#推导式
dict02 = {item: item**2 for item in range(1,11)}
print(dict02)

#只记录大于5的数字
dict01 = {}
for item in range(1,11):
    if item >5:
        dict01[item] = item**2

dict02 = {item: item**2 for item in range(1,11) if item >5}
print(dict02)

'''
练习：["无忌","赵敏","周芷若"]
-> {"无忌":2,"赵敏":2,"周芷若":3}
练习：["无忌","赵敏","周芷若"]
     [101,102,103]
     {"无忌":101,"赵敏":102,"周芷若":103,}
'''
list01 = ["无忌","赵敏","周芷若"]
dict01 = {item: len(item) for item in list01}
print(dict01)

list01 = ["无忌","赵敏","周芷若"]
list02 = [101,102,103]
#通过索引同时在多个列表中获取元素：
for i in range(len(list01)):
    dict01[list01[i]] = list02[i]
dict02 = {list01[i]: list02[i] for i in range(len(list01))}
print(dict02)

#需求：字典如何根据value查找key
#解决方案1:键值互换
dict02 = {value: key for key,value in dict01.items()}
print(dict02)
#缺点  ：如果key重复，交换或则丢失数据

#如果需要保持所有数据
#[(k,v),]
list02 = [(key,value ) for key,value in dict01.items()]
print(list02)